Package-level declarations

Types

Link copied to clipboard
class Chat(val id: String = "chat", initialMessages: List<UIMessage> = emptyList(), transport: ChatTransport)
Link copied to clipboard
data class ChatRequest(val messages: List<UIMessage>, val body: Map<String, JsonElement> = emptyMap(), val headers: Map<String, String> = emptyMap())
Link copied to clipboard
class ChatSession(chat: Chat)
Link copied to clipboard
data class ChatState(val id: String, val messages: List<UIMessage> = emptyList(), val status: ChatStatus = ChatStatus.Ready, val error: Throwable? = null)
Link copied to clipboard
Link copied to clipboard
interface ChatTransport
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
class TextStreamChatTransport(handler: (ChatRequest) -> Flow<String>, assistantMessageId: (ChatRequest) -> String = { request -> getResponseUiMessageId(request.messages) }) : ChatTransport
Link copied to clipboard
data class TextStreamResponse(val textStream: Flow<String>, val status: Int = 200, val headers: Map<String, String> = textStreamHeaders())
Link copied to clipboard
@Serializable
enum TextUIPartState : Enum<TextUIPartState>

Streaming-vs-final state for UIMessagePart.Text and UIMessagePart.Reasoning (per historical parity gap #30, mirrors v6's TextUIPart.state: 'streaming' | 'done'). Renderers use this to drive a typing cursor / fade animation while the model is still producing the part, and to commit a final layout pass on Done.

Link copied to clipboard
@Serializable
enum ToolCallState : Enum<ToolCallState>

Tool call lifecycle in the UI. v6 names — input-streaming / input-available / output-available, NOT v5's partial-call / call / result.

Link copied to clipboard

Registry of per-tool renderers. The SDK is UI-framework-agnostic — the registry holds renderer functions that take UIToolInvocation. An application can supply Compose composables, SwiftUI bridge closures, server-rendered nodes, or any other renderer value.

Link copied to clipboard
@Serializable
data class UIMessage(val id: String, val role: UIMessageRole, val parts: List<UIMessagePart>, val createdAtMs: Long = 0, val metadata: Map<String, JsonElement>? = null)

UI-shape message — what the chat surface renders. Carries an ordered list of UIMessageParts (Vercel AI SDK v6 message-parts model). The conversion from ai.torad.aisdk.StreamEvent flow into a growing list of these messages happens in streamToUiMessages.

Link copied to clipboard
@Serializable
sealed interface UIMessagePart
Link copied to clipboard
@Serializable
enum UIMessageRole : Enum<UIMessageRole>
Link copied to clipboard
data class UIMessageStreamResponse(val stream: Flow<UIMessage>, val status: Int = 200, val headers: Map<String, String> = uiMessageStreamHeaders())
Link copied to clipboard
Link copied to clipboard
class UIToolInvocation<TInput, TOutput>(val toolCallId: String, val toolName: String, val state: ToolCallState, val input: TInput?, val output: TOutput?, val error: String?)

Typed invocation handle — what a per-tool renderer receives. Carries the typed input + output via the tool's own serializers.

Functions

Link copied to clipboard
Link copied to clipboard
fun chatSession(id: String = "chat", initialMessages: List<UIMessage> = emptyList(), transport: ChatTransport): ChatSession
Link copied to clipboard
fun convertToModelMessages(messages: List<UIMessage>, ignoreIncompleteToolCalls: Boolean = false): List<ModelMessage>

Convert a list of UI-shape UIMessages back into the model-shape ModelMessage list the LLM expects on the next turn. Mirrors v6's convertToModelMessages (per historical parity gap #5).

Link copied to clipboard
fun createTextStreamResponse(textStream: Flow<String>, status: Int = 200, headers: Map<String, String> = textStreamHeaders()): TextStreamResponse
Link copied to clipboard
fun createUiMessageStream(onError: (Throwable) -> UIMessage = { throwable -> UIMessage( id = "error", role = UIMessageRole.Assistant, parts = listOf(UIMessagePart.Error(throwable.message ?: "stream failed")), ) }, execute: suspend UIMessageStreamWriter.() -> Unit): Flow<UIMessage>
Link copied to clipboard
fun createUiMessageStreamResponse(stream: Flow<UIMessage>, status: Int = 200, headers: Map<String, String> = uiMessageStreamHeaders()): UIMessageStreamResponse
Link copied to clipboard
fun <TData> dataAs(part: UIMessagePart.Data, serializer: KSerializer<TData>): TData
Link copied to clipboard
Link copied to clipboard
fun <TInput> dynamicInputAs(part: UIMessagePart.DynamicToolUI, serializer: KSerializer<TInput>): TInput?
Link copied to clipboard
fun <TOutput> dynamicOutputAs(part: UIMessagePart.DynamicToolUI, serializer: KSerializer<TOutput>): TOutput?
Link copied to clipboard
fun getResponseUiMessageId(messages: List<UIMessage>, createId: () -> String = { "msg_${messages.size + 1}" }): String
Link copied to clipboard
fun handleUiMessageStreamFinish(messages: List<UIMessage>, onFinish: (List<UIMessage>) -> Unit)
Link copied to clipboard
fun <TInput> inputAs(part: UIMessagePart.ToolUI, serializer: KSerializer<TInput>): TInput?

Type-safe extraction of input args while the tool is mid-flight or complete. Returns null if input isn't yet available.

Link copied to clipboard
Link copied to clipboard
fun <TMetadata> UIMessage.metadataAs(name: String, serializer: KSerializer<TMetadata>): TMetadata?
Link copied to clipboard
fun <TOutput> outputAs(part: UIMessagePart.ToolUI, serializer: KSerializer<TOutput>): TOutput?

Type-safe extraction at the UI render seam. Pulls a typed TOutput out of UIMessagePart.ToolUI.output. Returns null if state isn't OutputAvailable (caller should branch on state first).

Link copied to clipboard
Link copied to clipboard
suspend fun pipeTextStreamToResponse(textStream: Flow<String>, response: ServerResponseWriter, status: Int = 200, headers: Map<String, String> = textStreamHeaders())
Link copied to clipboard
suspend fun pipeUiMessageStreamToResponse(stream: Flow<UIMessage>, response: ServerResponseWriter, encoder: (UIMessage) -> String = { it.toString() }, status: Int = 200, headers: Map<String, String> = uiMessageStreamHeaders())
Link copied to clipboard
fun readUiMessageStream(stream: Flow<UIMessage>): Flow<UIMessage>
Link copied to clipboard
fun streamToUiMessages(events: Flow<StreamEvent>, assistantMessageId: String): Flow<UIMessage>

Convert a raw agent StreamEvent flow into a flow of growing UIMessage snapshots. Each emission is the current full state of the assistant message being built — caller's StateFlow<List<UIMessage>> just replaces the in-flight message on each emission.

Link copied to clipboard
fun textStreamFromEvents(events: Flow<StreamEvent>): Flow<String>
Link copied to clipboard
Link copied to clipboard
fun toUIMessageStream(events: Flow<StreamEvent>): Flow<JsonObject>

Bridge an agent StreamEvent flow to the v6 UI-message-stream wire protocol: each emission is a UIMessageChunk JSON object (e.g. { "type": "text-delta", "id": "...", "delta": "..." }) ready to be framed as an SSE data: line and consumed by a JS useChat client. Events with no wire counterpart (response-metadata, tool-input-end, raw) are dropped.

Link copied to clipboard
fun transformTextToUiMessageStream(textStream: Flow<String>, assistantMessageId: String, metadata: Map<String, JsonElement>? = null): Flow<UIMessage>
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard